package com.bingo.dbutils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.jws.soap.InitParam;
import javax.sql.DataSource;

import com.bingo.jdbc.JdbcUtils;

public class QR<T> {
	/**
	 * 实现 insert、update、delete
	 * @param sql
	 * @param params
	 * @return
	 */
	private DataSource dataSource;
	
	public QR() {
		super();
	}
	public QR(DataSource dataSource) {
		super();
		this.dataSource = dataSource;
	}

	public int update(String sql,Object... params){
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try {
			con=dataSource.getConnection();//通过连接池得到连接对象
			
			pstmt = con.prepareStatement(sql);//使用sql创建pstmt
			InitParams(pstmt,params);//设置参数
			
			return pstmt.executeUpdate();//执行
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			try {
				if (pstmt!=null) pstmt.close();
				if (con!=null) con.close();
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
		
		}

	}
	
	//给参数赋值
	private void InitParams(PreparedStatement pstmt, Object... params) throws SQLException {
		// TODO Auto-generated method stub
		for(int i=0;i<params.length;i++){
			pstmt.setObject(i+1, params[i]);
		}
	}
	public T query(String sql,RsHandler<T> rh,Object... params){
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con=dataSource.getConnection();//通过连接池得到连接对象
			
			pstmt = con.prepareStatement(sql);//使用sql创建pstmt
			InitParams(pstmt,params);//设置参数
			
			 rs = pstmt.executeQuery();//执行
			 return rh.handle(rs);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			try {
				if (pstmt!=null) pstmt.close();
				if (con!=null) con.close();
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
		
		}
	}
}

//用来把结果集转换成需要的类型
interface RsHandler<T>{
	public T handle(ResultSet rs) throws SQLException;
}
